package com.aaa.cs.controller;

import com.aaa.cs.entity.MaintainRecord;
import com.aaa.cs.service.MaintainRecordService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.entity.MaintainRecord;
import com.aaa.cs.service.MaintainRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (MaintainRecord)表控制层
 *
 * @author makejava
 * @since 2020-11-24 21:14:39
 */
@RestController
@RequestMapping("maintainRecord")
public class MaintainRecordController {
    /**
     * 服务对象
     */
    @Resource
    private MaintainRecordService maintainRecordService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.maintainRecordService.queryById(id));
    }

    /**
     * 全部新增
     * @param maintainRecord
     * @return
     */
    @PostMapping("insertMaintainRecord")
    public Result insertMaintainRecord(@RequestBody MaintainRecord maintainRecord){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),maintainRecordService.insert(maintainRecord));
    }

    /**
     * 修改数据
     * @param maintainRecord
     * @return
     */
    @PutMapping("updateMaintainRecord")
    public Result updateMaintainRecord(@RequestBody MaintainRecord maintainRecord){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),maintainRecordService.update(maintainRecord));
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteMaintainRecord")
    public Result deleteMaintainRecord(Integer id){
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),maintainRecordService.deleteById(id));
    }

    /**
     * 分页与模糊检索
     * @param pageNo
     * @param pageSize
     * @param maintainRecord
     * @return
     */
    @GetMapping("queryAll")
    public Result queryAll(Integer pageNo, Integer pageSize, MaintainRecord maintainRecord){
        return maintainRecordService.queryAll(pageNo,pageSize,maintainRecord);
    }


}